package com.zjdiepu.www.manage.api.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.common.util.ZhppDateUtil;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.base.IDpBaseDealerInfoService;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.ibiz.third.IJuheCityService;
import com.zjdiepu.www.ibiz.third.IJuheProvinceService;
import com.zjdiepu.www.model.base.dto.DpBaseDealerInfo;
import com.zjdiepu.www.model.base.ext.DpBaseDealerInfoExt;
import com.zjdiepu.www.model.base.vo.DpBaseDealerInfoVo;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;

/**
 * 经销商表Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月21日 10时30分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DealerInfoController", produces="application/json;charset=UTF-8")
@Api(tags = {"经销商表"}, produces="application/json;charset=UTF-8")
public class DpBaseDealerInfoController extends BaseController {
    @Autowired
    private IDpBaseDealerInfoService dpBaseDealerInfoService;
    @Autowired
    private IBaseBusiUnitService baseBusiUnitService;
    @Autowired
    private IJuheProvinceService juheProvinceService;
    @Autowired
    private IJuheCityService juheCityService;
    @Resource
    private IDpBaseDataDictionaryService iDpBaseDataDictionaryService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeDealerInfo  <br/>
     * @Description: 修改经销商 <br/>
     * @param busiId
     * @param dealerId
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改经销商", response = DpBaseDealerInfo.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "changeDealerInfo", method = RequestMethod.POST)
    public ApiResponse changeDealerInfo(String busiId, String dealerId) {
    	getCurrentUser();
    	if(StringUtils.isBlank(busiId) || StringUtils.isBlank(dealerId)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	DpBaseDealerInfo dpBadeDealerInfo = dpBaseDealerInfoService.saveChangeDealerInfo(busiId, dealerId);
        return ApiResponse.success(dpBadeDealerInfo);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增经销商表 <br/>
     * @param dpBaseDealerInfo
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增经销商表", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DpBaseDealerInfo dpBaseDealerInfo) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dpBaseDealerInfo.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dpBaseDealerInfo.setModifierName(currentUser.getName());
        dpBaseDealerInfoService.saveUpdateAndDelete(dpBaseDealerInfo, OperateType.add);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除经销商表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除经销商表", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	DpBaseDealerInfo dpBaseDealerInfo = new DpBaseDealerInfo();
    	dpBaseDealerInfo.setId(id);
        dpBaseDealerInfoService.saveUpdateAndDelete(dpBaseDealerInfo, OperateType.delete);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改经销商表 <br/>
     * @param dpBaseDealerInfo
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改经销商表", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DpBaseDealerInfo dpBaseDealerInfo) {
    	AbstractSysUser currentUser = getCurrentUser();
    	dpBaseDealerInfo.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dpBaseDealerInfo.setModifierName(currentUser.getName());
    	dpBaseDealerInfoService.saveUpdateAndDelete(dpBaseDealerInfo, OperateType.edit);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询经销商表<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询经销商表", response = DpBaseDealerInfo.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DpBaseDealerInfo dpBaseDealerInfo = dpBaseDealerInfoService.selectById(id);
       if(null == dpBaseDealerInfo) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dpBaseDealerInfo);
    }

    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询经销商表 <br/>
     * @param dpBaseDealerInfoExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询经销商表",response = DpBaseDealerInfo.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DpBaseDealerInfoExt dpBaseDealerInfoExt) {
    	String sqlXmls = "";
		Date submitTimeStart = dpBaseDealerInfoExt.getSubmitTimeStart();
		Date submitTimeEnd = dpBaseDealerInfoExt.getSubmitTimeEnd();
		if(null != submitTimeStart && null != submitTimeEnd){
			long start = DateUtil.truncAndAddDays(submitTimeStart, 0).getTime() / 1000;
			long end = DateUtil.truncAndAddDays(submitTimeEnd, 1).getTime() / 1000;
			sqlXmls = " AND `CREATED` >= "+start+" AND `CREATED` < "+end;
			dpBaseDealerInfoExt.setWhereSql(sqlXmls);
		} else if(null != submitTimeStart) {
			long start = DateUtil.truncAndAddDays(submitTimeStart, 0).getTime() / 1000;
			sqlXmls = " AND `CREATED` >= " + start;
		} else if(null != submitTimeEnd) {
			long end = DateUtil.truncAndAddDays(submitTimeEnd, 1).getTime() / 1000;
			sqlXmls = " AND `CREATED` < "+end;;
		}
    	
        dpBaseDealerInfoExt.notDeleted();
        dpBaseDealerInfoExt.setWhereSql(sqlXmls);
        PageVo page = getPage(dpBaseDealerInfoExt);
        int records = dpBaseDealerInfoService.countByExt(dpBaseDealerInfoExt);
        page.setRecords(records);
        if (records > 0) {
            List<DpBaseDealerInfo> dpBaseDataDictionaries = dpBaseDealerInfoService.selectByExt(dpBaseDealerInfoExt);
            for(int i=0;i<dpBaseDataDictionaries.size();i++) {
            	DpBaseDealerInfo dealerInfo = dpBaseDataDictionaries.get(i);
            	String dealerProvince = dealerInfo.getDealerProvince();
            	String province = juheProvinceService.getNameById(dealerProvince);
            	dealerInfo.setDealerProvinceDisplay(province);
            	String dealerCity = dealerInfo.getDealerCity();
            	String city = juheCityService.getNameById(dealerCity);
            	dealerInfo.setDealerCityDisplay(city);
            	String unitId = dealerInfo.getUnitId();
        		BaseBusiUnit baseBusiUnit = baseBusiUnitService.selectById(unitId);
        		if(null != baseBusiUnit) {
        			dealerInfo.setUnitName(baseBusiUnit.getOrgName());
        		}
            	
            	dealerInfo.setCreatedDisplay(ZhppDateUtil.stampToDate(dealerInfo.getCreated(), ZhppDateUtil.SHORT_DATE_FORMAT));
            }
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    @ApiOperation(value = "经销商表关联经销商详情表",response = DpBaseDealerInfoVo.class, httpMethod="POST", tags={"列表"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getMulitListById", method = RequestMethod.POST)
    public ApiResponse getMulitListById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DpBaseDealerInfoVo dpBaseDealerInfo = dpBaseDealerInfoService.getMulitListById(id);
       if(null == dpBaseDealerInfo) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dpBaseDealerInfo);
    }

}
